package com.goofengcn.website.application.util.pager;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>Title: 网上审核辅助系统</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: 亿通国际 上海美华系统有限公司</p>
 *
 * @author 
 * @version 1.0
 */
public class BaseForm {
	public static final Double NaN = new Double(Double.NaN);

	private static final Log log = LogFactory.getLog(BaseForm.class);

	protected void prepareHidden(StringBuffer sb, String name, String value) {
		sb.append("\r<input");
		prepareAttribute(sb, "type", "hidden");
		prepareAttribute(sb, "name", name);
		prepareAttribute(sb, "value", value);
		sb.append(">");
	}

	protected void prepareAttribute(StringBuffer sb, String name, String value) {
		sb.append(" ");
		sb.append(name);
		sb.append("=");
		sb.append("\"");
		sb.append(value == null ? "" : value);
		sb.append("\"");
	}

	protected void prepareHref(StringBuffer sb, String name, String value) {
		if (sb.length() != 0) {
			sb.append("&");
		}
		sb.append(name);
		sb.append("=");
		sb.append(value == null ? "" : value);
	}

	/**
	 * 计算Iterator中对象属性的和
	 * @param col Collection
	 * @param property String 属性名称
	 * @return Double 和
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	protected Double calculateSum(Collection col, String property) {
		//log.debug("=====> calculateSum Collection");
		if (col == null) {
			return null;
		}
		return calculateSum(col.iterator(), property);
	}

	/**
	 * 计算Iterator中对象属性的和
	 * @param col Map
	 * @param property String 属性名称
	 * @return Double 和
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	protected Double calculateSum(Map col, String property) {
		//log.debug("=====> calculateSum Map");
		if (col == null) {
			return null;
		}
		return calculateSum(col.values().iterator(), property);
	}

	/**
	 * 计算Iterator中对象属性的和
	 * @param col Iterator
	 * @param property String 属性名称
	 * @return Double 和
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	protected Double calculateSum(Iterator col, String property) {
		//log.debug("=====> calculateSum Iterator");
		if (col == null) {
			return null;
		}

		Object obj = null;
		Object value = null;
		BigDecimal tmp_result = null;
		BigDecimal tmp_value = null;
		while (col.hasNext()) {
			obj = col.next();
			if (obj == null) {
				continue;
			}

			try {
				//        log.debug("=====================> " + obj.getClass().getName());
				//        if (obj instanceof com.metinform.gdzc.po.VGdzcProjectList)
				//        {
				//          com.metinform.gdzc.po.VGdzcProjectList abc = (com.metinform.gdzc.po.VGdzcProjectList)obj;
				//          log.debug("=====================> com.metinform.gdzc.po.VGdzcProjectList");
				//          log.debug("=====================> " + abc.getListid());
				//          log.debug("=====================> " + abc.getListDefaultSystemRate());
				//          log.debug("=====================> " + abc.getLogicFactFee());
				//        }
				value = PropertyUtils.getProperty(obj, property);
				if (value != null) {
					if (value instanceof Number) {
						tmp_value = new BigDecimal(((Number) value)
								.doubleValue());
						if (tmp_result == null) {
							tmp_result = tmp_value;
						} else {
							tmp_result = tmp_result.add(tmp_value);
						}
					} else {
						log.error("property:" + property
								+ " is not an instance of Number!");
					}
				}
			} catch (IllegalAccessException e) {
				log.error(e.getMessage(), e);
				return NaN;
			} catch (InvocationTargetException e) {
				log.error(e.getMessage(), e);
				return NaN;
			} catch (NoSuchMethodException e) {
				log.error(e.getMessage(), e);
				return NaN;
			}
		}
		return (tmp_result == null ? null
				: new Double(tmp_result.doubleValue()));
	}

}
